<?php

/**
 * student actions.
 *
 * @package    Projecte Final de Carrera
 * @subpackage student
 * @author     Jaume Fernandez
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class studentActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->students = Doctrine_Core::getTable('Student')
                ->createQuery('a')
                ->execute();
    }

    public function executeShow(sfWebRequest $request) {
        $this->student = Doctrine_Core::getTable('Student')->find(array($request->getParameter('id')));
        $this->forward404Unless($this->student);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new StudentForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new StudentForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($student = Doctrine_Core::getTable('Student')->find(array($request->getParameter('id'))), sprintf('Object student does not exist (%s).', $request->getParameter('id')));
        print_r($student->toArray());
        print_r($student->getUser()->getFirstName());
        $this->form = new StudentForm($student);

        $this->form->setDefault('username', $student->getUser()->getUsername());
        $this->form->setDefault('password', $student->getUser()->getPassword());
        $this->form->setDefault('first name', $student->getUser()->getFirstName());
        $this->form->setDefault('last name', $student->getUser()->getLastName());
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($student = Doctrine_Core::getTable('Student')->find(array($request->getParameter('id'))), sprintf('Object student does not exist (%s).', $request->getParameter('id')));
        $this->form = new StudentForm($student);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($student = Doctrine_Core::getTable('Student')->find(array($request->getParameter('id'))), sprintf('Object student does not exist (%s).', $request->getParameter('id')));
        $student->delete();

        $this->redirect('student/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
//----------------------
            print_r($form->__toString());
      //     die();
            $student = $form->save();
            echo '<br>';
            print_r($form->getValue('last name'));
            echo '<br>';
            print_r($student->getUser()->toArray());
            echo 'despres <br>';
       //     die();
            //Cal comprovar si ja existia abans, per no crear sfGardUser i nomes actualtizar-lo
            $user = new sfGuardUser();
            $user->setUsername($form->getValue('username'));
            $user->setFirstName($form->getValue('first name'));
            $user->setLastName($form->getValue('last name'));
            $user->setEmailAddress($form->getValue('username') . '@foo.bar');
            $user->setPassword($form->getValue('password'));
            
       //     $user->save();
            $student->setUser($user);
            
            $student->save();
            
         //   print_r($alumne->getUser()->toArray());
        //    die();
//-----------------------------
            $this->redirect('student/edit?id=' . $student->getId());
        }
    }

}

/*

class alumneActions extends sfActions {



    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
           print_r($form->__toString());
      //     die();

            $alumne = $form->save();
            echo '<br>';
            print_r($form->getValue('last name'));
            echo '<br>';
            print_r($alumne->getUser()->toArray());
            echo 'despres <br>';
       //     die();
            //Cal comprovar si ja existia abans, per no crear sfGardUser i nomes actualtizar-lo
            $user = new sfGuardUser();
            $user->setUsername($form->getValue('username'));
            $user->setFirstName($form->getValue('first name'));
            $user->setLastName($form->getValue('last name'));
            $user->setEmailAddress($form->getValue('username') . '@foo.bar');
            $user->setPassword($form->getValue('password'));
            
            
       //     $user->save();
            $alumne->setUser($user);
            
            $alumne->save();
            
         //   print_r($alumne->getUser()->toArray());
        //    die();

            $this->redirect('alumne/edit?id=' . $alumne->getId());
        }
    }

} 

 */